package org.frogs.algo;

public class Point2 {

	public double x;
    public double y;

    public Point2(double x, double y)
    {
        this.x = x;
        this.y = y;
    }
    
    public Point2(Point2 otherPoint)
    {
        this.x = otherPoint.x;
        this.y = otherPoint.y;
    }

    public static Point2 SumVectors(Point2 a, Point2 b)
    {
        return new Point2(a.x + b.x, a.y + b.y);
    }

    public static Point2 SubVectors(Point2 a, Point2 b)
    {
        return new Point2(a.x - b.x, a.y - b.y);
    }

    public static Point2 multiplyByCoef(Point2 a, double coef)
    {
        return new Point2(a.x * coef, a.y * coef);
    }
    
    public static double Magnitude(Point2 point)
    {
        return Math.sqrt((point.x * point.x) + (point.y * point.y));
    }
    
    public static Point2 getNormal(Point2 point1, Point2 point2) {
    	return Point2.normal(point1.x, point1.y, point2.x, point2.y);
    }
    
    //normal vector
    public static Point2 normal(double x1,double y1,double x2, double y2) {
	    double nx,ny;
	    if (x1 == x2) {
	      nx = Math.signum(y2-y1);
	      ny = 0;
	    } else {
	      double f = (y2-y1)/(double)(x2-x1);
	      nx = f*Math.signum(x2-x1)/Math.sqrt(1+f*f);
	      ny = -1*Math.signum(x2-x1)/Math.sqrt(1+f*f);
	    }
	    return new Point2(nx,ny);
    }
    
    public static Point2 getDirection(Point2 A, Point2 B) {
    	double a = B.x - A.x;
    	double b = B.y - A.y;
    	
    	return new Point2(a, b);
    }
    
    public static double getDistanceBetweenPoints(Point2 A, Point2 B) {
    	if (B == null) return 1000;
    	double result = Math.sqrt((B.y-A.y)*(B.y-A.y) + (B.x - A.x)*(B.x - A.x));
    	return result;
    }
    
    public String toString() {
    	return this.x + " " + this.y;
    }

}
